package band.wukong.practice.java.core.pack.io;

import java.io.*;
import java.util.Date;

/**
 * As you see...
 *
 * @author wukong(wukonggg@139.com)
 */
public class SerializableSample {

    public static void main(String[] args) {
//        serial();
//        deserial();
        byte[] lizhi = chenjia(new Person("李志", 40));
        System.out.println("lizhi = " + lizhi);
    }

    public static byte[] chenjia(Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        byte[] bytes = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            bytes = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes;
    }

    public static void serial() {
        Person person = new Person("Clapton", 70);

        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(new FileOutputStream("./SerializableSample.bin"));
            out.writeObject("万碎万碎万万碎");
            out.writeObject(new Date());
            out.writeObject(person);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void deserial() {
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(new FileInputStream("./SerializableSample.bin"));
            System.out.println("String: " + in.readObject());
            System.out.println("Date: " + in.readObject());
            System.out.println("Person: " + in.readObject());

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

class Person implements Serializable {

    private static final long serialVersionUID = 5062703761162581352L;
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
            "name='" + name + '\'' +
            ", age='" + age + '\'' +
            '}';
    }
}